package edu.mtu.citizenscience.cs.notepad.activity.noteslist;

import java.util.Calendar;

import edu.mtu.citizenscience.cs.notepad.activity.R;
import edu.mtu.citizenscience.cs.notepad.activity.noteslist.note.LocationActivity;
import edu.mtu.citizenscience.cs.notepad.activity.noteslist.note.PhotosActivity;
import edu.mtu.citizenscience.cs.notepad.activity.noteslist.note.RecordingActivity;
import edu.mtu.citizenscience.cs.notepad.activity.noteslist.note.SaveDialog;
import edu.mtu.citizenscience.cs.notepad.model.ModelNote;

import edu.mtu.citizenscience.cs.notepad.NPActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.MenuItem;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Note Activity Responsible for 
 * displaying and editing a note
 */
public class NoteActivity extends NPActivity {
	// Model which holds the data for this note
	private ModelNote note;
	// dialog that asks the user if they want to save the note when going back
	private DialogFragment saveDialog = new SaveDialog();

	/**
	 * Find Activity creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.activity_note);
		
		// get note id passed in the lanuch intent incase the user is editing a previous note
		long noteId = this.getIntent().getLongExtra(ModelNote.NOTE_ID, -1);
		
		// check to see if got note from the launch intent
		if (noteId > -1)
		{
			// find the note model for the passed id
			this.note = new ModelNote(this, noteId).find();
			
			// set the note title to the note models title for viewing and editing
			this.aq.id(R.id.noteTitle).text(this.note.getTitle());
			// set the note message to the note models message for viewing and editing
			this.aq.id(R.id.noteMessage).text(this.note.getMessage());
		}
		
		// set button click handlers for media attachment
		this.aq.id(R.id.imageAttachmentButton).clicked(this, "viewPhotos");
		this.aq.id(R.id.locationAttachmentButton).clicked(this, "viewLocation");
		this.aq.id(R.id.recordingAttachmentButton).clicked(this, "viewRecording");
	}
	
	/**
	 * Create the android menu
	 *
	 * @param   menu - Menu - The options menu in which you place your items.
	 * @return  boolean - true as we want the menu to be displayed
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		this.getMenuInflater().inflate(R.menu.note_menu, menu);
		
		return true;
	}
	
	/**
	 * Handle menu item selected
	 *
	 * @param   item - MenuItem - The menu item that was selected.
	 * @return  boolean - boolean Return false to allow normal menu processing to proceed, true to consume it here.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		 // Switch over menu options
		 switch (item.getItemId()) 
		 {
		 	case R.id.note_menu_save:
		 		// save the note
		 		this.save();
		 		return true;
		 	case R.id.note_menu_revert:
		 		// revert note changes
		 		this.revert();
		 		return true;
		 	case R.id.note_menu_delete:
		 		// delete the note
		 		this.delete();
		 		return true;
		 	default:
		 		return super.onOptionsItemSelected(item);
		 }
	}
	
	/**
	 * Handle menu item selected
	 *
	 * @param   item - MenuItem - The menu item that was selected.
	 * @return  boolean - boolean Return false to allow normal menu processing to proceed, true to consume it here.
	 */
	@Override
	public void onBackPressed() 
	{
		// get the note title from the text input
		String title = this.aq.id(R.id.noteTitle).getText().toString();
		// get the note message from the text input
		String message = this.aq.id(R.id.noteMessage).getText().toString();
		
		// if note has not been saved before or the title has changed or message has changed
		if (this.note == null || ! this.note.getTitle().equals(title) || ! this.note.getMessage().equals(message))
		{
			// show the save dialog to check if the user wants to save the new note or changes
			this.saveDialog.show(this.getSupportFragmentManager(), "dialog");
		}
		else
		{
			// since there is nothing to possibly save simply finish the activity
			this.finish();
		}	
	}
	
	/**
	 * Save the note to the DB
	 * 
	 * @param   void
	 * @return	void
	 */
	public void save()
	{
		// get the note title from the text input
		String title = this.aq.id(R.id.noteTitle).getText().toString();
		// get the note message from the text input
		String message = this.aq.id(R.id.noteMessage).getText().toString();
		
		// if the note model does not exist this is a new note
		if (this.note == null)
		{
			// create a new note model
			this.note = new ModelNote(this);
		}
		// set the note title, message, and set date to current time
		this.note.setTitle(title);
		this.note.setMessage(message);
		this.note.setDate(Calendar.getInstance().getTime());
		// save the note to the DB
		this.note.save();
	}
	
	/**
	 * onClick handler for photos button
	 * 
	 * @param   void
	 * @return	void
	 */
	public void viewPhotos()
	{
		// default note id to -1 for when the note is not saved
		long noteId = -1;
		
		// check that have a saved node
		if (this.note != null)
		{
			// save any changes to the note first
			this.note.save();
			
			// get the saved node id
			noteId = this.note.getId();
		}
		
		// launch photo activity passing the note id
		this.startActivity(new Intent(this, PhotosActivity.class).putExtra(ModelNote.NOTE_ID, noteId));
	}
	
	/**
	 * onClick handler for location button
	 * 
	 * @param   void
	 * @return	void
	 */
	public void viewLocation()
	{
		// default note id to -1 for when the note is not saved
		long noteId = -1;
		
		// check that have a saved node
		if (this.note != null)
		{
			// save any changes to the note first
			this.note.save();
			
			// get the saved node id
			noteId = this.note.getId();
		}
		
		// launch location activity passing the note id
		this.startActivity(new Intent(this, LocationActivity.class).putExtra(ModelNote.NOTE_ID, noteId));
	}
	
	/**
	 * onClick handler for recording button
	 * 
	 * @param   void
	 * @return	void
	 */
	public void viewRecording()
	{
		// default note id to -1 for when the note is not saved
		long noteId = -1;
		
		// check that have a saved node
		if (this.note != null)
		{
			// save any changes to the note first
			this.note.save();
			
			// get the saved node id
			noteId = this.note.getId();
		}
		
		// launch recording activity passing the note id
		this.startActivity(new Intent(this, RecordingActivity.class).putExtra(ModelNote.NOTE_ID, noteId));
	}
	
	/**
	 * Revert the note to the info stored in the note model
	 * 
	 * @param   void
	 * @return	void
	 */
	public void revert()
	{
		// check that have a saved node
		if (this.note != null)
		{
			// set the title text view to the title in the note model
			this.aq.id(R.id.noteTitle).text(this.note.getTitle());
			// set the message text view to the title in the note model
			this.aq.id(R.id.noteMessage).text(this.note.getMessage());
		}
	}
	
	/**
	 * Delete the note
	 * 
	 * @param   void
	 * @return	void
	 */
	public void delete()
	{
		// check that have a saved node
		if (this.note != null)
		{
			// delete the note model from the db
			this.note.delete();
			// FIX delete any attached media as well
		}
		
		// done with note so finish the activity
		this.finish();
	}
	
	/**
	 * onClick handler for yes clicked in save dialog
	 * 
	 * @param   void
	 * @return	void
	 */
	public void saveYesClicked()
	{
		// dismiss the save dialog
		this.saveDialog.dismiss();
		// save the note
		this.save();
		
		// done with note so finish the activity
		this.finish();
	}
	
	/**
	 * onClick handler for no clicked in save dialog
	 * 
	 * @param   void
	 * @return	void
	 */
	public void saveNoClicked()
	{
		// dismiss the save dialog
		this.saveDialog.dismiss();
		
		// done with note so finish the activity
		this.finish();
	}
	
}